Thank you for joining me in this lecture.
We're going to be taking a look at an object oriented design technique called composition.
Composition is a way for us to create complex objects and systems by combining simpler objects into
one object.
The main idea of composition is to have an object like, let's say, a car, exist with separate objects
that each behave and work on their own functionality.
As an example, if we were to create a car object, we would want separate objects within the car object
to do their own thing like the tires, the suspension, the engine, and so on.
The style of building complex objects can be easier to understand and is actually preferred by many
developers over inheritance.
So let's take a look at how we can use composition in Lua.
I'm going to create a class, and this class is simply just going to represent a 2D coordinate.
So we'll just call it coordinate.
And a coordinate needs an X property and it needs a Y property.
And we'll make sure to set the coordinate underscore underscore index meta method to the coordinate
class.
And then we can create a constructor for our coordinate class.
Pass an X and a Y position, and all we need to do is create a new table.
I'm going to call it self.
It's going to be equal.
To set meta table.
We're going to create a new table in here and attach the coordinate class to this table.
Then from that point we can set the x equal to the x pass to the function.
And we can set the y equal to the y pass to the function.
And then we can just return self.
Now I'm going to include one function in this class.
And this function is going to be called draw.
And all it's going to do is print.
Drawing coordinates at x will give the x coordinate.
And then we'll do it at the Y coordinate as well.
Now let's create a second class.
And this class I'm just going to call it complex object, just to represent our complex object.
And inside of this class we're going to have a property called components.
And in here we're going to store as many of these coordinate objects as we'd like inside of our complex
object.
And we're going to make sure to set the underscore underscore index meta method equal to the complex
objects table.
Let's also create a constructor for this complex object.
And now we're going to do is return the table from set meta table.
We're going to create a new table.
And we're going to set the complex object class as the meta table.
And then we can add a function to this class.
Like adding a coordinate.
To the components table.
So we'll pass a coordinate to this function.
And all this function is going to do is insert into the components property this coordinate.
And now we can have a function that draws all the coordinates that are inside of the components table.
So we can do complex object.
Draw coordinates.
And all this function is going to do is loop through every single coordinate that is in the components
table.
And it's going to.
Call the draw function on that coordinate, which belongs to the coordinate class.
So now what we could do is we could create a new complex object.
And then we can insert some coordinates inside of the complex object.
So a complex object.
And then we can call the add coordinate function and create a new coordinate.
We'll just set this one to maybe like three and 11.
And then we could do another one.
Create a new coordinate.
And for this one we'll just do like nine and negative four.
And then when we go to call the.
Draw coordinates function.
It should print three and 11 and nine and four in the console.
So what's going on here is that we're creating a new complex object from our complex object class,
and we're just inserting other objects that came from a different class into our complex object, our
complex objects.
Kind of vague, but I'm just kind of giving you an example of how composition works.
You could also imagine our complex object as a car and us we're inserting, you know, the different
objects to represent maybe the tires or the engines and things like that.
So once we insert all of these new coordinate objects inside of our complex object, they get stored
within the components table.
And when we call the draw coordinates function, it's going to go through each one of those coordinate
objects and call the draw function on them which exists within the coordinate table.
So it should print each of the coordinates that belong to our complex object.
And let me make sure to fix that real quick right there.
Got to make sure to concatenate to the string.
So if we go and run our game, we should get two statements printed into the console.
And there we go drawing coordinates at x three, y 11 and drawing coordinates at x nine and y negative
four.
And we could also do a for loop for I equals 1 to 100.
What we'll do is we'll create a bunch of new coordinates to insert into our table.
So we can just do something like math dot random.
And or actually what we'll do is we'll use random dot new.
And we'll get an integer between.
I don't know.
-1001 thousand.
And we'll do the same thing here for the y coordinate.
After we generate a bunch of different coordinates, then it should print 100 different statements into
the console.
So if we run it.
There we go.
We get 100 different coordinates that are stored in our complex object.
So as you can see, composition can be a lot easier to understand than inheritance.
Instead of inheriting classes into other classes, we're just putting objects into other objects.
That's just kind of the main idea of composition.
Nothing too complex about it.
It just allows us to break down complex objects into smaller and smaller, more simpler objects, like
having a car object, and then you can break it down into its other components like the doors, the
tires, the engine, the spring, and so on.
So I hope you enjoyed this lecture and I'll see you in the next one.